package com.feidu.fragment;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.feidu.utils.PictureUtils;
/**
 * 在DialogFragment 中显示大图片
 * @author MV
 *
 */
public class ImageFragment extends DialogFragment {

	public static final String EXTRA_IMAGE_PATH = "com.android.criminalintent.image_path";
	
	private ImageView mImageView;
	
	public static ImageFragment newInstance(String imagePath) {
		
		Bundle args = new Bundle();
		args.putSerializable(EXTRA_IMAGE_PATH, imagePath);
		
		ImageFragment fragment = new ImageFragment();
		fragment.setArguments(args);
		fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
		
		return fragment;
		
	}
	/**
	 * 方法创建ImageView并从argument获取文路径。然后获取缩小版的图片并设置给ImageView
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		mImageView = new ImageView(getActivity());
		String path = (String)getArguments().getSerializable(EXTRA_IMAGE_PATH);
		
		BitmapDrawable image = PictureUtils.getScaleDrawable(getActivity(), path);
		mImageView.setImageDrawable(image);
		
		return mImageView;
	}
	/**
	 * 只要图片不再需要，就主动覆盖onDestroyView()方法以释放内存
	 */
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		
		PictureUtils.cleanImageView(mImageView);
	}
}
